﻿namespace DepartmentEditForm
{
    using System;
    using System.Collections.Generic;
    using System.Text.RegularExpressions;

    using Microsoft.SharePoint;

    public class DepartmentInformationBL
    {
        #region Fields

        List<DepartmentFields> _AvailableFields;
        List<DepartmentInformation> _DepartmentList;

        #endregion Fields

        #region Properties

        public List<DepartmentFields> AvailableFields
        {
            get
            {
                if ( _AvailableFields == null )
                    _AvailableFields = new List<DepartmentFields>( );
                return _AvailableFields;
            }
            set
            {
                _AvailableFields = value;
            }
        }

        public List<DepartmentInformation> DepartmentList
        {
            get
            {
                if ( _DepartmentList == null )
                    _DepartmentList = new List<DepartmentInformation>( );
                return _DepartmentList;
            }
            set
            {
                _DepartmentList = value;
            }
        }

        #endregion Properties

        #region Methods

        public void GetNonDepartmentFields( SPWeb storeWeb, string sourceDepartment )
        {
            DepartmentFields deptFields = new DepartmentFields( );

            SPList currentDepartment = null;
            SPList departmentListValues = storeWeb.Lists[ "Department Administration List" ];
            SPQuery nonUserDepartments = new SPQuery( );

            nonUserDepartments.Query = "<Where><Eq><FieldRef Name='Department_x0020_Name' /><Value Type='Text'>" + sourceDepartment.Trim( ) + "</Value>   </Eq></Where>";
            nonUserDepartments.ViewFields = "<FieldRef Name='Department_x0020_Name'/>";
            SPListItemCollection otherDepartments = departmentListValues.GetItems( nonUserDepartments );
            foreach ( SPItem departmentItem in otherDepartments )
            {
                currentDepartment = storeWeb.Lists[ departmentItem[ "Department List Name" ].ToString( ).Trim( ) ];
                foreach ( SPField departmentField in currentDepartment.Fields )
                {
                    if ( !departmentField.Hidden && departmentField.FieldTypeDefinition.UserCreatable )
                    {
                        deptFields = new DepartmentFields( );
                        deptFields.DepartmentName = departmentItem[ "Department Name" ].ToString( ).Trim( );
                        deptFields.FieldName = departmentField.Title.Trim( );
                        deptFields.InternalFieldName = departmentField.InternalName.Trim( );

                        AvailableFields.Add( deptFields );
                    }
                }
            }
        }

        public void GetNonDepartmentFields( string siteURL, string sourceDepartment )
        {
            using ( SPSite storeSite = new SPSite( siteURL ) )
            {
                DepartmentFields deptFields = new DepartmentFields( );
                using ( SPWeb storeWeb = storeSite.OpenWeb( ) )
                {
                    SPList currentDepartment = null;
                    SPList departmentListValues = storeWeb.Lists[ "Department Administration List" ];
                    SPQuery nonUserDepartments = new SPQuery( );

                    nonUserDepartments.Query = "<Where><Eq><FieldRef Name='Department_x0020_Name' /><Value Type='Text'>" + sourceDepartment.Trim( ) + "</Value>   </Eq></Where>";
                    SPListItemCollection otherDepartments = departmentListValues.GetItems( nonUserDepartments );
                    foreach ( SPItem departmentItem in otherDepartments )
                    {
                        currentDepartment = storeWeb.Lists[ departmentItem[ "Department List Name" ].ToString( ).Trim( ) ];
                        foreach ( SPField departmentField in currentDepartment.Fields )
                        {
                            if ( !departmentField.Hidden && departmentField.FieldTypeDefinition.UserCreatable )
                            {
                                deptFields = new DepartmentFields( );
                                deptFields.DepartmentName = departmentItem[ "Department Name" ].ToString( ).Trim( );
                                deptFields.FieldName = departmentField.Title.Trim( );
                                deptFields.InternalFieldName = departmentField.InternalName.Trim( );
                                AvailableFields.Add( deptFields );
                            }
                        }
                    }
                }
            }
        }

        public void LoadDepartments( SPWeb storeWeb )
        {
            DepartmentInformation singleDepartment = new DepartmentInformation( );

            SPList departmentListValues = storeWeb.Lists[ "Department Administration List" ];
            foreach ( SPListItem item in departmentListValues.Items )
            {
                singleDepartment = new DepartmentInformation( );
                if ( item[ "Department Name" ] != null )
                    singleDepartment.DepartmentName = item[ "Department Name" ].ToString( );
                if ( item[ "Department List Name" ] != null )
                    singleDepartment.ListName = item[ "Department List Name" ].ToString( );
                if ( item[ "Department Security Group" ] != null )
                    singleDepartment.SecurityGroup = item[ "Department Security Group" ].ToString( );
                if ( item[ "Department Grid Code" ] != null )
                    singleDepartment.DepartmentCode = item[ "Department Grid Code" ].ToString( );

                DepartmentList.Add( singleDepartment );
            }
        }

        public void LoadDepartments( string siteURL )
        {
            using ( SPSite storeSite = new SPSite( siteURL ) )
            {
                DepartmentInformation singleDepartment = new DepartmentInformation( );
                using ( SPWeb storeWeb = storeSite.OpenWeb( ) )
                {
                    SPList departmentListValues = storeWeb.Lists[ "Department Administration List" ];
                    foreach ( SPListItem item in departmentListValues.Items )
                    {
                        singleDepartment = new DepartmentInformation( );
                        if ( item[ "Department Name" ] != null )
                            singleDepartment.DepartmentName = item[ "Department Name" ].ToString( );
                        if ( item[ "Department List Name" ] != null )
                            singleDepartment.ListName = item[ "Department List Name" ].ToString( );
                        if ( item[ "Department Security Group" ] != null )
                            singleDepartment.SecurityGroup = item[ "Department Security Group" ].ToString( );

                        DepartmentList.Add( singleDepartment );
                    }
                }
            }
        }

        public List<DepartmentViewReferences> SelectUniqueColumnDepartmentsValues( SPWeb storeWeb, string sourceDepartment )
        {
            SPQuery nonUserDepartments = new SPQuery( );
            DepartmentViewReferences deptFields = new DepartmentViewReferences( );
            deptFields = null;
            string fieldsBuild = "<FieldRef Name='ID' />";
            List<DepartmentViewReferences> allFieldsByDepartment = new List<DepartmentViewReferences>( );
            SPList departmentListValues = storeWeb.Lists[ "Department Ordering Lookup" ];
            nonUserDepartments.Query = "<Where><And><Eq><FieldRef Name='Department_x0020_Owner' /><Value Type='Text'>" + sourceDepartment + "</Value></Eq><Neq><FieldRef Name='Source_x0020_Department' /><Value Type='Text'>" + sourceDepartment + "</Value></Neq></And></Where><OrderBy><FieldRef Name='Source_x0020_Department' Ascending='True' /><FieldRef Name='Field_x0020_Name' Ascending='True' /></OrderBy>";
            SPListItemCollection otherDepartments = departmentListValues.GetItems( nonUserDepartments );
            string currentDepartment = "";

            foreach ( SPListItem item in otherDepartments )
            {
                if ( !currentDepartment.Equals( item[ "Source Department" ].ToString( ) ) )
                {
                    if ( currentDepartment != string.Empty )
                    {
                        deptFields.AllFields = fieldsBuild;
                        allFieldsByDepartment.Add( deptFields );
                    }
                    deptFields = new DepartmentViewReferences( );
                    currentDepartment = item[ "Source Department" ].ToString( );
                    if ( currentDepartment.Equals( "Stores Detail" ) )
                        fieldsBuild = "<FieldRef Name='Reference_x0020__x0023_' />" + "<FieldRef Name='" + item[ "Internal Field Name" ].ToString( ) + "' />";
                    else
                        fieldsBuild = "<FieldRef Name='Store_x0020_Reference_x0020_ID' />" + "<FieldRef Name='" + item[ "Internal Field Name" ].ToString( ) + "' />";

                    deptFields.DepartmentName = currentDepartment;
                }
                else
                {
                    if ( item[ "Field Name" ].ToString( ).Equals( "Modified By" ) )
                        fieldsBuild += "<FieldRef Name='Editor' />";
                    else
                        fieldsBuild += "<FieldRef Name='" + item[ "Internal Field Name" ].ToString( ) + "' />";
                }
            }

            deptFields.AllFields = fieldsBuild;
            allFieldsByDepartment.Add( deptFields );
            return allFieldsByDepartment;
        }

        public string Strip( string text )
        {
            return Regex.Replace( text, @"<(.|\n)*?>", string.Empty );
        }

        #endregion Methods
    }
}